package com.yzh.wanandroid.utils

import android.content.Context
import android.util.Base64
import com.yzh.wanandroid.component.app.MainApplication
import java.io.*


object SpUtils {

    /**
     * 保存的文件名字
     */
    private const val PREFERENCE_NAME = "wan_android"

    fun putString(key: String, value: String): Boolean {
        val sp =
            MainApplication.appContext.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE)
        val editor = sp.edit()
        editor.putString(key, value)
        return editor.commit()
    }

    fun getString(key: String, defaultValue: String?): String? {
        val sp =
            MainApplication.appContext.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE)
        return sp.getString(key, defaultValue)
    }

    fun putInt(key: String, value: Int): Boolean {
        val sp =
            MainApplication.appContext.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE)
        val editor = sp.edit()
        editor.putInt(key, value)
        return editor.commit()
    }

    fun getInt(key: String, defaultValue: Int): Int {
        val sp =
            MainApplication.appContext.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE)
        return sp.getInt(key, defaultValue)
    }

    fun putLong(key: String, value: Long): Boolean {
        val sp =
            MainApplication.appContext.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE)
        val editor = sp.edit()
        editor.putLong(key, value)
        return editor.commit()
    }

    fun getLong(key: String, defaultValue: Long): Long {
        val sp =
            MainApplication.appContext.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE)
        return sp.getLong(key, defaultValue)
    }

    fun putFloat(key: String, value: Float): Boolean {
        val sp =
            MainApplication.appContext.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE)
        val editor = sp.edit()
        editor.putFloat(key, value)
        return editor.commit()
    }

    fun getFloat(key: String, defaultValue: Float): Float {
        val sp =
            MainApplication.appContext.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE)
        return sp.getFloat(key, defaultValue)
    }

    fun putBoolean(key: String, value: Boolean): Boolean {
        val sp =
            MainApplication.appContext.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE)
        val editor = sp.edit()
        editor.putBoolean(key, value)
        return editor.commit()
    }

    fun getBoolean(key: String, defaultValue: Boolean): Boolean {
        val sp =
            MainApplication.appContext.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE)
        return sp.getBoolean(key, defaultValue)
    }

    fun remove(key: String) {
        val sp =
            MainApplication.appContext.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE)
        val editor = sp.edit()
        editor.remove(key)
        editor.commit()
    }

    fun clearAll() :Boolean{
        val sp =
            MainApplication.appContext.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE)
        val editor = sp.edit()
        editor.clear()
        return editor.commit()
    }

    fun contains(key: String): Boolean {
        val sp =
            MainApplication.appContext.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE)
        return sp.contains(key)
    }

    fun getAll(): Map<String, *> {
        val sp =
            MainApplication.appContext.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE)
        return sp.all
    }

    fun putObject(key: String, any: Any): Boolean {
        val byteArrayOs = ByteArrayOutputStream()
        try {
            val objectOs = ObjectOutputStream(byteArrayOs)
            objectOs.writeObject(any)
            val value = String(Base64.encode(byteArrayOs.toByteArray(), Base64.DEFAULT))
            val sp = MainApplication.appContext.getSharedPreferences(
                PREFERENCE_NAME,
                Context.MODE_PRIVATE
            )
            val editor = sp.edit()
            editor.putString(key, value)
            return editor.commit()
        } catch (e: IOException) {
            e.printStackTrace()
        }
        return false
    }

    fun getObject(key: String): Any? {
        val value = getString(key, null)
        if (value != null) {
            val valueBytes = Base64.decode(value, Base64.DEFAULT)
            val bin = ByteArrayInputStream(valueBytes)
            return try {
                val oin = ObjectInputStream(bin)
                oin.readObject()
            } catch (e: Exception) {
                null
            }
        }
        return null
    }
}
